package cn.iocoder.yudao.module.product.property.controller.app;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.product.property.dal.dataobject.ProductPropertyValueDO;
import cn.iocoder.yudao.module.product.property.service.ProductPropertyValueService;
import cn.iocoder.yudao.module.product.property.vo.AppProductPropertyValueRespVO;
import cn.iocoder.yudao.module.product.property.vo.AppProductPropertyValueSaveReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 产品 SKU值")
@RestController
@RequestMapping("/product/property/value")
@Validated
public class AppProductPropertyValueController {

    @Resource
    private ProductPropertyValueService productPropertyValueService;

    @PostMapping("/create")
    @Operation(summary = "创建规格值")
    public CommonResult<Long> createPropertyValue(@Valid @RequestBody AppProductPropertyValueSaveReqVO createReqVO) {
        return success(productPropertyValueService.createPropertyValue(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新规格值")
    public CommonResult<Boolean> updatePropertyValue(@Valid @RequestBody AppProductPropertyValueSaveReqVO updateReqVO) {
        productPropertyValueService.updatePropertyValue(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除规格值")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<Boolean> deletePropertyValue(@RequestParam("id") Long id) {
        productPropertyValueService.deletePropertyValuePropertyId(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得规格值")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppProductPropertyValueRespVO> getPropertyValue(@RequestParam("id") Long id) {
        ProductPropertyValueDO value = productPropertyValueService.getPropertyValue(id);
        return success(BeanUtils.toBean(value, AppProductPropertyValueRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得规格值列表")
    @Parameter(name = "propertyId", description = "规格项编号", required = true, example = "1024")
    public CommonResult<List<AppProductPropertyValueRespVO>> getPropertyValueList(@RequestParam("propertyId") Long propertyId) {
        List<ProductPropertyValueDO> list = productPropertyValueService.getPropertyValueListByPropertyIds(CollUtil.newArrayList(propertyId));
        return success(BeanUtils.toBean(list, AppProductPropertyValueRespVO.class));
    }

}
